package com.hk.huakangcompany.service.impl;

import com.hk.huakangcompany.model.WxPermission;
import com.hk.huakangcompany.model.WxUserRoleRelation;
import com.hk.huakangcompany.model.vo.LoginAdmin;
import com.hk.huakangcompany.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/21 19:48
 * @Statement: 组件类
 */
@Slf4j
@Service
public class CustemUserDetailsService implements UserDetailsService {

    @Autowired
    private AdminService adminService;

    @Autowired
    private WxUserRoleRelationService userRoleRelationService;

    @Autowired
    private WxRoleService roleService;

    @Autowired
    private WxRolePermissionRelationService rolePermissionRelationService;

    @Autowired
    private WxPermissionService permissionService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        log.error("CustemUserDetailsService.loadUserByUsername()");
//        通过username获取用户信息
        System.out.println("email==="+email);
        LoginAdmin admin = adminService.queryByEmail(email);
        if (admin == null) {
            throw new UsernameNotFoundException("账户不存在");
        }
        /**
         * 根据用户ID获取授权的权限
         * 1、通过用户ID查询用户所有角色Id
         * 2、通过用户所属角色Id查出角色所属权限Id
         * 3、通过权限Id查询角色所属权限
         */
//        1、通过用户ID查询用户所有角色
        List<WxUserRoleRelation> userRoleRelationList = userRoleRelationService.findByAdminId(admin.getId());
//        判断是否具有角色
        if (userRoleRelationList.size() == 0) {
            //        存储权限集合
            List<GrantedAuthority> authorities = new ArrayList<>();
            //        账户名，密码，权限
            LoginAdmin loginAdmin = new LoginAdmin(admin.getId(),admin.getName(),admin.getEmail(),passwordEncoder.encode(admin.getPassword()),authorities);
            return loginAdmin;
        }
//        得到用户所属角色ID集合
        List<Integer> roleIds = userRoleRelationList.stream().map(WxUserRoleRelation::getRoleId).collect(Collectors.toList());
//        通过角色ID查询角色
        /*List<WxRole> roleList = roleService.findByIds(roleIds);
        List<String> rNameList = roleList.stream().map(WxRole::getName).collect(Collectors.toList());*/
//        2、通过用户所属角色Id查出角色所属权限Id
        Set<Integer> permissionIds = rolePermissionRelationService.findByRoleIds(roleIds);
//        3、通过权限Id查询角色所属权限
        List<WxPermission> permissionList = permissionService.findByIds(permissionIds);
        List<String> authoritie = permissionList.stream().map(WxPermission::getValue).collect(Collectors.toList());
//        存储权限集合
        List<GrantedAuthority> authorities = new ArrayList<>();
//        角色
        /*for (String rName : rNameList) {
            authorities.add(new SimpleGrantedAuthority("ROLE_"+rName));
        }*/
        //       权限
        for (String s : authoritie) {
            authorities.add(new SimpleGrantedAuthority(s));
        }
        LoginAdmin loginAdmin = new LoginAdmin(admin.getId(),admin.getName(),admin.getEmail(),passwordEncoder.encode(admin.getPassword()),authorities);
        return loginAdmin;
    }

}
